package com.geeklei.media.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.widget.RelativeLayout;

import com.geeklei.media.ui.R;
import com.geeklei.media.ui.common.AndroidUtils;

public class GestureBrightness extends GestureView {

    public GestureBrightness(Context context) {
        super(context);
    }

    public static GestureBrightness create(RelativeLayout relativeLayout) {
        GestureBrightness gestureBrightness = new GestureBrightness(relativeLayout.getContext());
        gestureBrightness.setLayoutParams(new LayoutParams(-1, -1));
        relativeLayout.addView(gestureBrightness);
        return gestureBrightness;
    }

    public void adjustBrightness(Activity activity, float f) {
        int activityBrightness = (int) (AndroidUtils.getActivityBrightness(activity) * 255.0f);
        if (activityBrightness < 0) {
            activityBrightness = AndroidUtils.getSystemBrightness(activity);
        }
        activityBrightness = getNewBrightnessValue(f, activityBrightness);
        AndroidUtils.setActivityBrightness(activity, activityBrightness);
        setPercent((activityBrightness * 100) / MotionEventCompat.ACTION_MASK);
        show();
    }

    private void setPercent(int i) {
        this.mText.setText(i + "%");
    }

    private int getNewBrightnessValue(float f, int i) {
        int i2 = MotionEventCompat.ACTION_MASK;
        int r2 = f > 0.0f ? i - 17 : f < 0.0f ? i + 17 : i;
        if (r2 <= MotionEventCompat.ACTION_MASK) {
            i2 = r2;
        }
        if (i2 < 2) {
            return 2;
        }
        return i2;
    }

    protected int getIcon() {
        return R.drawable.vp_light_icon_big;
    }

    protected int getIconMarginTop() {
        return getContext().getResources().getDimensionPixelSize(R.dimen.vp_brightness_icon_margin_top);
    }

    protected int getTextMarginTop() {
        return getContext().getResources().getDimensionPixelSize(R.dimen.vp_brightness_percent_margin_top);
    }
}
